package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import com.wpen.unit.infra.repository.job.JobUndoPlanRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.JobUndoPlanMapper;
import com.wpen.unit.infra.mybatis.job.po.JobUndoPlanPo;
import com.wpen.unit.infra.mybatis.job.po.JobUndoPlanPo.JobUndoPlanColumns;
import com.wpen.unit.utils.StringUtil;

/**
 * 任务作业未执行维护.
 * 
 * @author Wang Peng
 * @date 2022-12-11
 *
 */
@Repository
@Transactional
public class JobUndoPlanRepositoryImpl extends ServiceImpl<JobUndoPlanMapper, JobUndoPlanPo>
        implements JobUndoPlanRepository {

    @Override
    public JobUndoPlanPo queryByIdWithLock(Serializable id) {
        QueryWrapper<JobUndoPlanPo> queryWrapper = Wrappers.query();
        queryWrapper.eq(JobUndoPlanColumns.ID.name(), id);
        queryWrapper.last(" FOR UPDATE");
        return super.getOne(queryWrapper);
    }

    @Override
    public List<JobUndoPlanPo> queryListByTriggerTime(int size, String azCode) {
        QueryWrapper<JobUndoPlanPo> queryWrapper = Wrappers.query();
        queryWrapper.lt(JobUndoPlanColumns.JOB_TRIGGER_TIME.name(), System.currentTimeMillis() + 2000);
        queryWrapper.orderByAsc(true, JobUndoPlanColumns.JOB_TRIGGER_TIME.name());
        Page<JobUndoPlanPo> page = new Page<>(0, size, false);
        return super.page(page, queryWrapper).getRecords();
    }

    @Override
    public long count(JobUndoPlanPo jobUndoPlanPo) {
        QueryWrapper<JobUndoPlanPo> queryWrapper = createQuery(jobUndoPlanPo);
        return super.count(queryWrapper);
    }

    @Override
    public List<JobUndoPlanPo> queryList(int index, int size, JobUndoPlanPo jobUndoPlanPo) {
        QueryWrapper<JobUndoPlanPo> queryWrapper = createQuery(jobUndoPlanPo);
        if (size > 0) {
            Page<JobUndoPlanPo> page = new Page<>(index, size, false);
            return super.page(page, queryWrapper).getRecords();
        }
        // queryWrapper.orderBy(true, false, JobUndoPlanColumns.ID.name());
        return getBaseMapper().selectList(queryWrapper);
    }

    private QueryWrapper<JobUndoPlanPo> createQuery(JobUndoPlanPo jobUndoPlanPo) {
        QueryWrapper<JobUndoPlanPo> queryWrapper = Wrappers.query();
        if (jobUndoPlanPo.getId() != null) {
            queryWrapper.eq(JobUndoPlanColumns.ID.name(), jobUndoPlanPo.getId());
        }
        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAppMode())) {
            queryWrapper.eq(JobUndoPlanColumns.APP_MODE.name(), jobUndoPlanPo.getAppMode());
        }
        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAppJarName())) {
            queryWrapper.eq(JobUndoPlanColumns.APP_JAR_NAME.name(), jobUndoPlanPo.getAppJarName());
        }
        if (jobUndoPlanPo.getJobTriggerTime() != null) {
            queryWrapper.eq(JobUndoPlanColumns.JOB_TRIGGER_TIME.name(), jobUndoPlanPo.getJobTriggerTime());
        }
        if (StringUtil.isNotEmpty(jobUndoPlanPo.getExecStatus())) {
            queryWrapper.eq(JobUndoPlanColumns.EXEC_STATUS.name(), jobUndoPlanPo.getExecStatus());
        }
        return queryWrapper;
    }

    @Override
    public JobUndoPlanPo queryFirst(JobUndoPlanPo jobUndoPlanPo) {
        QueryWrapper<JobUndoPlanPo> queryWrapper = Wrappers.query();
        if (jobUndoPlanPo.getId() != null) {
            queryWrapper.eq(JobUndoPlanColumns.ID.name(), jobUndoPlanPo.getId());
        }
        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAppMode())) {
            queryWrapper.eq(JobUndoPlanColumns.APP_MODE.name(), jobUndoPlanPo.getAppMode());
        }
        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAppJarName())) {
            queryWrapper.eq(JobUndoPlanColumns.APP_JAR_NAME.name(), jobUndoPlanPo.getAppJarName());
        }
        if (jobUndoPlanPo.getJobTriggerTime() != null) {
            queryWrapper.eq(JobUndoPlanColumns.JOB_TRIGGER_TIME.name(), jobUndoPlanPo.getJobTriggerTime());
        }
        if (StringUtil.isNotEmpty(jobUndoPlanPo.getExecStatus())) {
            queryWrapper.eq(JobUndoPlanColumns.EXEC_STATUS.name(), jobUndoPlanPo.getExecStatus());
        }
        // queryWrapper.orderBy(true, false, JobUndoPlanPoColumns.ID.name());
        return super.getOne(queryWrapper);
    }

    @Override
    public JobUndoPlanPo queryById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean insert(JobUndoPlanPo jobUndoPlanPo) {
//        jobUndoPlanPo.setVersion(1);
//        jobUndoPlanPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(jobUndoPlanPo);
    }

    @Override
    public boolean insertBatch(List<JobUndoPlanPo> listJobUndoPlanPos) {
        int rs = getBaseMapper().insertBatchSomeColumn(listJobUndoPlanPos);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(JobUndoPlanPo jobUndoPlanPo) {
        UpdateWrapper<JobUndoPlanPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(JobUndoPlanColumns.ID.name(), jobUndoPlanPo.getId());
//        updateWrapper.eq(JobUndoPlanColumns.VERSION.name(), jobUndoPlanPo.getVersion());
//        jobUndoPlanPo.setVersion(jobUndoPlanPo.getVersion() + 1);
        boolean rs = super.update(jobUndoPlanPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(ucpBatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<JobUndoPlanPo> updateWrapper = Wrappers.update();
        updateWrapper.in(JobUndoPlanColumns.ID.name(), (Object[]) ids);
        return super.remove(updateWrapper);
    }

//    public JobUndoPlanPo queryByExample(JobUndoPlanPo jobUndoPlanPo) {
//        QueryWrapper<JobUndoPlanPo> queryWrapper = Wrappers.query();
//        if (jobUndoPlanPo.getId() != null) {
//            queryWrapper.eq(JobUndoPlanColumns.ID.name(), jobUndoPlanPo.getId());
//        }
//        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAppMode())) {
//            queryWrapper.eq(JobUndoPlanColumns.APP_MODE.name(), jobUndoPlanPo.getAppMode());
//        }
//        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAzCode())) {
//            queryWrapper.eq(JobUndoPlanColumns.AZ_CODE.name(), jobUndoPlanPo.getAzCode());
//        }
//        if (StringUtil.isNotEmpty(jobUndoPlanPo.getAppJarName())) {
//            queryWrapper.eq(JobUndoPlanColumns.APP_JAR_NAME.name(), jobUndoPlanPo.getAppJarName());
//        }
//        if (jobUndoPlanPo.getJobTriggerTime() != null) {
//            queryWrapper.eq(JobUndoPlanColumns.JOB_TRIGGER_TIME.name(), jobUndoPlanPo.getJobTriggerTime());
//        }
//        if (StringUtil.isNotEmpty(jobUndoPlanPo.getExecStatus())) {
//            queryWrapper.eq(JobUndoPlanColumns.EXEC_STATUS.name(), jobUndoPlanPo.getExecStatus());
//        }
//		Page<JobUndoPlanPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, JobUndoPlanPoColumns.ID.name());
//    }

}
